<template>
    <view class="page">
       <view class="container">
            <view class="container_main">
                <view class="container_main_s" >
                    <view class="container_main_s_title">
                        请输入支付密码
                    </view>
                    <view class="container_main_s_pas" @tap='KeyboarOpen'>
                        <password-input :numLng='password'></password-input>
                    </view>
                    <view style="margin:35px auto;display:block;width: 80%;">
                        <button class="button1" @tap="reset_pwd()">
                            确认并提交
                        </button>
                    </view>
                </view>
            </view> 
            <number-keyboard tabBar ref='KeyboarHid' @input='onInput' psdLength='6'></number-keyboard>
       	</view>
       </view>
    </view>
</template>

<script>
    import numberKeyboard from '@/components/number-keyboard/number-keyboard.vue'
    import passwordInput from '@/components/password-input/password-input.vue'

    import api from "../../library/index.js"
    import headerline from '../../components/header.vue'
    import http from "../../library/http.js"
    import uniIcons from '../../components/uni-icons/uni-icons.vue'
    var statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
    export default {
        components: {
            uniIcons,
            headerline,
            numberKeyboard,
            passwordInput
        },
        data() {
            return {

                statusBarHeight: statusBarHeight + 'px',
                navitem: ['修改资金密码', '忘记资金密码'],
                navselect: 0,
                navstyle: "width:60%;left:20%",
                user: uni.getStorageSync('userInfo'),
                system: uni.getStorageSync('system'),
                isfrist: false,
                pwd0: '',
                pwd1: '',
                pwd2: '',
                pwd: '',
                step: 1,
                mobile: '',
                mobileshow: '',
                type: 'pwd',
                timer: null,
                getCodeTime: 60,
                disabled: false,
                issending: false,
                randcode: '',
                password: '',
                password1:'',
                band:false
            }
        },

        methods: {
            //打开键盘
            KeyboarOpen(e) {
            	this.$refs.KeyboarHid.open();
            },
            onInput(val) {
            	this.password = val;
            	if (this.password.length == 6) {
                    this.step = 2
                    this.$refs.KeyboarHid.close()
            	}
            },
    
            tapnav(num) {

                if (num == 1 && this.user.mobile == '') {
                    uni.showModal({
                        title: '提示',
                        content: "绑定手机号,才可以重置密码",
                        showCancel: true,
                        cancelText: '关闭',
                        confirmText: '绑定手机号',
                        success: res => {
                            if (res.confirm) {
                                uni.redirectTo({
                                    url: "/pages/mine/mobile"
                                })
                            }
                        }
                    });
                } else {
                    this.navselect = num;
                    if (num == 1) {
                        this.setuserdata();
                        this.getuserinfo();
                    }
                }
            },
            change_pwd() {

                if (!this.isfrist && this.pwd0 == '') {
                    uni.showToast({
                        icon: 'none',
                        title: '请输入支付密码',
                        duration: 1000
                    })

                    return false;
                }


                // if (this.pwd1 == '') {
                //     uni.showToast({
                //         icon: 'none',
                //         title: '请输入支付密码',
                //         duration: 1000
                //     })

                //     return false;
                // }

                if (this.password.length < 6) {
                    uni.showToast({
                        icon: 'none',
                        title: '新密码长度不能小于',
                        duration: 1000
                    })

                    return false;
                }
                // if (this.pwd1 != this.pwd2) {
                //     uni.showToast({
                //         icon: 'none',
                //         title: '两次密码输入不一致',
                //         duration: 1000
                //     })
                //     return false;
                // }


                http.setWait(true).post('user.php?act=change_pwd', {
                    method: 'safe',
                    id: this.user.id,
                    password: this.password,
                    oldpwd: this.pwd0,
                    isfrist: this.isfrist
                }).then(res => {

                    this.pwd0 = this.pwd1 = this.pwd2 = '';
                    uni.showToast({
                        icon: 'none',
                        title: '操作成功',
                        duration: 1000
                    })
                })



            },
            sendsms() {
                var mobile = this.mobile;
                var reg = /^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/;
                if (reg.test(mobile)) {

                    this.disabled = true;
                    this.issending = true;
                    http.setWait(true).post('user.php?act=sendCode', {
                        mobile: mobile,
                        type: this.type
                    }).then(res => {
                        this.issending = false;
                        if (res.data.method != 'error') {

                            var that = this;
                            clearInterval(that.timer);
                            that.timer = setInterval(function() {
                                that.getCodeTime--;
                                that.disabled = true;
                                if (that.getCodeTime <= 0) {
                                    that.disabled = false;
                                    that.getCodeTime = 60;
                                    clearInterval(that.timer);
                                }
                            }, 1000)

                        } else {
                            var that = this;
                            that.disabled = false;
                            that.getCodeTime = 60;
                            clearInterval(that.timer);
                            uni.showToast({
                                icon: 'none',
                                title: '短信发送失败',
                                duration: 1000
                            })
                        }

                    })

                } else {
                    uni.showToast({
                        icon: 'none',
                        title: '手机号码格式错误',
                        duration: 1000
                    })

                    return false;
                }

            },
            setuserdata() {
                if (this.user.mobile) {
                    this.mobile = this.user.mobile;
                    this.mobileshow = this.mobile.substr(0, 3) + "*****" + this.mobile.substr(this.mobile.length - 3,
                    3);


                }

            },
            getuserinfo() {
                var postdata = {
                    id: uni.getStorageSync('access_token')
                };
                http.setWait(false).post('user.php?act=userinfo', postdata).then(res => {
                    this.user = res.data;
                    uni.setStorageSync('userInfo', this.user)
                    this.setuserdata();
                })
            },
            reset_pwd() {
                var mobile = this.mobile;
                // if (mobile == '') {
                //     uni.showToast({
                //         icon: 'none',
                //         title: '手机号不能为空',
                //         duration: 1000
                //     })

                //     return false;
                // }
                // var reg = /^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/;
                // if (!reg.test(mobile)) {
                //     uni.showToast({
                //         icon: 'none',
                //         title: '手机号码格式错误',
                //         duration: 1000
                //     })

                //     return false;
                // }
                // if (this.randcode == '') {
                //     uni.showToast({
                //         icon: 'none',
                //         title: '短信验证码不能为空',
                //         duration: 1000
                //     })

                //     return false;
                // }
                if (this.password == '') {
                    uni.showToast({
                        icon: 'none',
                        title: '请输入支付密码',
                        duration: 1000
                    })

                    return false;
                }

                // if (this.pwd.length < 6) {
                //     uni.showToast({
                //         icon: 'none',
                //         title: '新密码长度不能小于6',
                //         duration: 1000
                //     })

                //     return false;
                // }

                http.setWait(true).post('user.php?act=reset_pwd', {
                    id: this.user.id,
                    mobile: mobile,
                    randcode: this.randcode,
                    pwd: this.password,
                    method: 'safe'
                }).then(res => {

                    this.pwd = this.randcode = '';
                    uni.showToast({
                        icon: 'none',
                        title: '操作成功',
                        duration: 1000
                    })
                    if(this.band){
                        uni.navigateBack({delta:1})
                    }
                })



            }
        },
        onLoad(opts) {
            var pwd = this.user.pwd1;
            if(opts.band){
                this.band = opts.band
            }
            if (pwd == undefined || pwd == null || pwd == '') {
                this.isfrist = true;
                this.navitem = ['设置支付密码', '忘记支付密码']
            } else {
                this.isfrist = false;
            }
            setTimeout(() => {
            	this.$refs.KeyboarHid.open()
            }, 50)

        }
    }
</script>

<style lang="scss" scoped>
    @import "@/static/css/user.css";

    .page {
        background-color: #fafafa;
    }

    .profile,
    .login-lines1 {
        background-color: #fff;
        // margin: 10px auto;
        width: 100%;
        vertical-align: middle;
        padding: 10upx 30upx;
        border-bottom: 1px solid #eee;
    }

    .profile li {
        vertical-align: middle;
    }

    .profile li:first-child {
        width: 100px;
        text-align: justify;
        text-align-last: justify;
    }

    .profile li:last-child {
        width: calc(100% - 130px)
    }
    
    .container {
    	overflow: hidden;
    
    	.container_main {
    
    		margin: 20rpx 30rpx;
    		background-color: #fff;
    		border-radius: 20rpx;
    
    		.container_main_s {
    			margin: 0 30rpx;
    			padding: 50rpx 0;
    
    			.container_main_s_title {
    				text-align: center;
    				font-weight: 600;
    				margin-bottom: 100rpx;
    			}
    		}
    	}
    }
</style>